from pydantic import BaseModel, Field, validator
from typing import Optional
from decimal import Decimal
from datetime import datetime

class WalletBase(BaseModel):
    name: str = Field(..., min_length=1, max_length=50, description="钱包名称")
    type: str = Field(..., description="钱包类型")
    balance: Decimal = Field(..., ge=0, description="余额")
    remark: Optional[str] = Field(None, max_length=200, description="备注")

    @validator('type')
    def validate_type(cls, v):
        allowed_types = {'cash', 'debit', 'credit', 'alipay', 'wechat', 'other'}
        if v not in allowed_types:
            raise ValueError(f'钱包类型必须是以下之一: {", ".join(allowed_types)}')
        return v

    @validator('balance')
    def validate_balance(cls, v):
        if v < 0:
            raise ValueError('余额不能为负数')
        return v

class WalletCreate(WalletBase):
    pass

class WalletUpdate(BaseModel):
    name: Optional[str] = Field(None, min_length=1, max_length=50)
    type: Optional[str] = None
    balance: Optional[Decimal] = Field(None, ge=0)
    remark: Optional[str] = Field(None, max_length=200)

class WalletInDB(WalletBase):
    id: int
    user_id: int

    class Config:
        from_attributes = True

class Wallet(WalletBase):
    id: int
    user_id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True 